function bindEvents(bindings) {
    for (var i in bindings) {
        if (bindings[i].selector) {
            $$(bindings[i].element).on(bindings[i].event, bindings[i].selector, bindings[i].handler);
        } else {
            $$(bindings[i].element).on(bindings[i].event, bindings[i].handler);
        }
    }
}

var batchQuery = {
    count: 10,
    counts: false,
    timer: null,
    imgList: [],
    nameArry: [],
    initialize: function () {
        this.bindEvent()
        this.init()
    },
    init: function () {
    },
    bindEvent: function () {
        var bindings = [
            {
                'element': '.page',
                'event': 'click',
                'selector': '.callphone',
                'handler': this.callphone()
            }, {
                'element': '.page',
                'event': 'click',
                'selector': '.vercode-button',
                'handler': this.getVerCode()
            }, {
                'element': '.page',
                'event': 'focusout',
                'selector': '.license',
                'handler': this.verLicense()
            }, {
                'element': '.page',
                'event': 'focusout',
                'selector': '.address',
                'handler': this.verAddress()
            }, {
                'element': '.page',
                'event': 'focusout',
                'selector': '.phone',
                'handler': this.verPhone()
            }, {
                'element': '.page',
                'event': 'focusout',
                'selector': '.vercode',
                'handler': this.verCode()
            }, {
                'element': '.page',
                'event': 'change',
                'selector': '#add-img',
                'handler': this.upload()
            }, {
                'element': '.page',
                'event': 'click',
                'selector': '.upload',
                'handler': this.shangchuan()
            }, {
                'element': '.page',
                'event': 'click',
                'selector': '.close',
                'handler': this.handlerClose()
            }
        ];
        utils.bindEvent(bindings);
    },
    verLicense: function () {
        return function () {
            var license = $('.license').val()
            if (!license) {
                $('.verif-text').html('请输入挡路车牌号')
                return true
            }
            $('.verif-text').html('')
        }
    },
    verAddress: function () {
        return function () {
            var address = $('.address').val()
            if (!address) {
                $('.verif-text').html('请输入车辆位置')
                return true
            }
            $('.verif-text').html('')
        }
    },
    verPhone: function () {
        return function () {
            var phone = $('.phone').val()
            if (!phone) {
                $('.verif-text').html('请输入您的电话')
                return true
            }
            $('.verif-text').html('')
        }
    },
    verCode: function () {
        return function () {
            var vercode = $('.vercode').val()
            if (!vercode) {
                $('.verif-text').html('请输入短信验证码')
                return true
            }
            $('.verif-text').html('')
        }
    },
    callphone: function () {
        var self = this
        return function () {
            if (self.verLicense()()) return
            if (self.verAddress()()) return
            if (self.verPhone()()) return
            if (self.verCode()()) return

            console.log('表单验证通过')

        }
    },
    getVerCode: function () {
        const self = this
        return function () {
            if (self.counts) return
            var phone = $('.phone').val();
            if (!phone) {
                myApp.alert('请输入您的手机电话')
                return
            }

            // 通过手机号码，发送ajax请求，获取验证码
            self.counts = true
            $('.vercode-button').html(self.count)

            self.timer = setInterval(function () {
                if (self.count === 0) {
                    clearInterval(self.timer)
                    $('.vercode-button').html('获取验证码')
                    self.counts = false
                    return
                }
                self.count--
                $('.vercode-button').html(self.count)
            }, 1000)
        }
    },
    upload: function () {
        const self = this;
        return function (e) {
            const file = e.target.files[0]
            var reader = new FileReader();
            // reader.readAsBinaryString(file)
            reader.readAsDataURL(file)
            reader.onloadend = function () {
                self.nameArry.push(file.name)

                self.imgList.push(reader.result)

                var ele = [
                    '<div class="photo-show" >',
                    '<span class="close" data-name="' + file.name + '" >x',
                    '</span>',
                    '<img src="' + reader.result + '" />',
                    '</div>'
                ]

                $('.upload-photo').append(ele.join(' '))
            }
        }
    },
    shangchuan: function () {
        var self = this;
        return function () {
            if (self.nameArry.length === 2) return
            $('#add-img').click()
        }
    },
    handlerClose: function () {
        var self = this
        return function () {
            var name = $(this).data('name'),
                index = self.nameArry.indexOf(name);

            self.nameArry.splice(name, 1)
            $(this).parent().remove()
        }
    }
}
batchQuery.initialize()